package com.zoke.support.image;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.zoke.support.R;
import com.zoke.support.base.BasePagerAdapter;
import com.zoke.support.config.ZokeConfig;
import com.zoke.support.image.activity.PhotoBrowserActivity;
import com.zoke.support.image.activity.PhotoBrowserActivity.PhotoTabFinishedListener;
import com.zoke.support.photoview.lib.PhotoView;
import com.zoke.support.photoview.lib.PhotoViewAttacher.OnPhotoTapListener;
import com.zoke.support.utils.MessageHandlerList;
import com.zoke.support.view.CircleProgress;

/**
 * 大图查看数据适配器
 * 
 * @author JackWu
 * @version
 * @date 2014-10-30
 * @_QQ_ 651319154
 */
public class ImageBrowserAdapter extends BasePagerAdapter {

	private PhotoTabFinishedListener mListener;

	public ImageBrowserAdapter(Context context, List<? extends Object> datas) {
		super(context, datas);
	}

	public ImageBrowserAdapter(Context context, List<? extends Object> datas,
			PhotoTabFinishedListener mListener) {
		super(context, datas);
		this.mListener = mListener;
	}

	@Override
	public View getItemView(ViewGroup container, int position) {
		View view = mInflater.inflate(R.layout.viewpager_item_photosbrower,
				null);

		PhotoView mPhotoView = (PhotoView) view
				.findViewById(R.id.photobrowser_pv);

		final CircleProgress mProgress = (CircleProgress) view
				.findViewById(R.id.roundBar);

		String url = (String) mDatas.get(position);
		ImageLoader.getInstance().displayImage(url, mPhotoView,
				ImageOptions.getPhotoOptions(),
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						super.onLoadingComplete(imageUri, view, loadedImage);
						mProgress.setVisibility(View.GONE);
					}
				}, new ImageLoadingProgressListener() {
					@Override
					public void onProgressUpdate(String imageUri, View view,
							int current, int total) {
						// 设置当前进度
						mProgress.setMainProgress(current);
					}
				});

		mPhotoView.setOnPhotoTapListener(new OnPhotoTapListener() {
			@Override
			public void onPhotoTap(View view, float x, float y) {
				ImageLoader.getInstance().clearMemoryCache();
				MessageHandlerList.sendMessage(PhotoBrowserActivity.class,
						ZokeConfig.ALL_UI_FINISH);
				if (mListener != null)
					mListener.finishActivity();
			}
		});

		return view;
	}

}
